﻿namespace EFS.RC.CC.CH.Tables
{
	public static class SurveySessionInfos
	{
		/// Returns an object from a data row
		public static EFS.RC.CC.CH.Entities.Surveys.Sessions.SessionInfo FromTableRow(System.Data.DataRow row)
		{
			// check
			EFS.Common.Verify.Reference(row, "row");

			// create new
			EFS.RC.CC.CH.Entities.Surveys.Sessions.SessionInfo dataObj = new EFS.RC.CC.CH.Entities.Surveys.Sessions.SessionInfo();
						//session
			// ID
			dataObj.Session.ID = EFS.DB.DataRow.Int32(row, "sessionID");

			// OCCURRED
			dataObj.Session.Occurred = EFS.DB.DataRow.DateTime(row, "OCCURRED");

			// SURVEY_ID
			dataObj.Session.SurveyId = EFS.DB.DataRow.Int32(row, "SURVEY_ID");

			// CONVERSATION_GUID
			dataObj.Session.ConversationGuid = EFS.DB.DataRow.Guid(row, "CONVERSATION_GUID");

			//SURVEY_STOP
			dataObj.Session.Survey_Stop = EFS.DB.DataRow.String(row, "SURVEY_STOP");

			//SURVEY_START
			dataObj.Session.Survey_Start = EFS.DB.DataRow.String(row, "SURVEY_START");

			//SURVEY_CONTINUE
			dataObj.Session.Survey_Continue = EFS.DB.DataRow.String(row, "SURVEY_CONTINUE");

			//SURVEY_CONTINUE
			dataObj.Session.DocumentId = EFS.DB.DataRow.String(row, "DOCUMENT_ID");

			//survey
			//id
			dataObj.Survey.ID = EFS.DB.DataRow.Int32(row, "surveyID");

			//name
			dataObj.Survey.Name = EFS.DB.DataRow.String(row, "NAME");

			//template
			dataObj.Survey.IsTemplate = (EFS.DB.DataRow.Int32(row, "TEMPLATE") == 1) ? "Yes" : "No";

			// TYPE
			System.Int32? nType = EFS.DB.DataRow.Int32(row, "TYPE");
			dataObj.Survey.Type = null;
			if (nType.HasValue)
			{
				dataObj.Survey.Type = (EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eType)nType.Value;
			}

			// REVIEW_TYPE
			System.Int32? nReviewType = EFS.DB.DataRow.Int32(row, "REVIEW_TYPE");
			dataObj.Survey.ReviewType = null;
			if (nReviewType.HasValue)
			{
				dataObj.Survey.ReviewType = (EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eReviewType)nReviewType.Value;
			}
			
			//user
			//logon_name
			dataObj.LogonName = EFS.DB.DataRow.String(row, "LOGON_NAME");

			//firstname
			dataObj.UserFirstName = EFS.DB.DataRow.String(row, "FIRST_NAME");

			//lastname
			dataObj.UserLastName = EFS.DB.DataRow.String(row, "LAST_NAME");

			//iscompleted
			dataObj.IsCompleted = EFS.DB.DataRow.Int32(row, "IS_COMPLETED");

			//read user name as well 

			// ok
			return dataObj;
		}

		/// <summary>
		/// Get all surveys for conversation
		/// </summary>
		/// <param name="conversationGuid"></param>
		/// <param name="username"></param>
		/// <returns></returns>
		public static EFS.RC.CC.CH.Entities.Surveys.Sessions.SessionInfo[] GetAllConversationSurveySessions(
			System.Guid conversationGuid,
			System.String username,
			EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eReviewType? reviewType)
		{
			// handle connection
			using (EFS.DB.Connection connection = EFS.RC.CC.CH.QRDConnection.New)
			{
				// get data
				EFS.RC.CC.CH.Entities.Surveys.Sessions.SessionInfo[] surveys = connection.DoExecuteTableReaderQuery<EFS.RC.CC.CH.Entities.Surveys.Sessions.SessionInfo>(
					"[dbo].[SurveySessions_GetAllConversationSurveys]",
					new EFS.DB.Parameters.Array(
						new System.String[] { "conversation_guid", "reviewType" },
						new System.Object[] { conversationGuid, (System.Int32?)reviewType },
						new System.Boolean[] { false, false }),
					FromTableRow);

				foreach (EFS.RC.CC.CH.Entities.Surveys.Sessions.SessionInfo survey in surveys)
				{
					if (username == survey.LogonName)
					{
						survey.ReadOnly = false;
					}
					else
					{
						survey.ReadOnly = true;
					}
				}

				return surveys;
			}
		}

		/// <summary>
		/// Get all surveys for document
		/// </summary>
		/// <param name="documentId"></param>
		/// <param name="username"></param>
		/// <returns></returns>
		public static EFS.RC.CC.CH.Entities.Surveys.Sessions.SessionInfo[] GetAllDocumentSurveySessions(
			System.String documentId,
			System.String username,
			EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eReviewType? reviewType)
		{
			// handle connection
			using (EFS.DB.Connection connection = EFS.RC.CC.CH.QRDConnection.New)
			{
				// get data
				EFS.RC.CC.CH.Entities.Surveys.Sessions.SessionInfo[] surveys = connection.DoExecuteTableReaderQuery<EFS.RC.CC.CH.Entities.Surveys.Sessions.SessionInfo>(
					"[dbo].[SurveySessions_GetAllDocumentSurveys]",
					new EFS.DB.Parameters.Array(
						new System.String[] { "document_id", "reviewType" },
						new System.Object[] { documentId, (System.Int32?)reviewType },
						new System.Boolean[] { false, false }),
					FromTableRow);

				foreach (EFS.RC.CC.CH.Entities.Surveys.Sessions.SessionInfo survey in surveys)
				{
					if (username == survey.LogonName)
					{
						survey.ReadOnly = false;
					}
					else
					{
						survey.ReadOnly = true;
					}
				}

				return surveys;
			}
		}
	}

}